#pragma once
#include <iostream>
#include <assert.h>
using namespace std;

namespace exercise
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
		{}

		vector(const vector<T>& v)
		{
			reserve(v.size());
			for (auto& e : v)
			{
				push_back(e);
			}
		}

		vector(size_t n, const T& val = T())
		{
			reserve(n);
			for (size_t i = 0;i < n;i++)
			{
				push_back(val);
			}
		}

		template<class InputIterator>
		vector(InputIterator begin, InputIterator end)
		{
			while (begin < end)
			{
				push_back(*begin);
				begin++;
			}
		}

		vector<T>& operator=(vector<T> v)
		{
			swap(v);
			return *this;
		}

		vector(initializer_list<T> l1)
		{
			reserve(l1.size());
			for (auto& e : l1)
			{
				push_back(e);
			}
		}

		void swap(vector<T>& temp)
		{
			std::swap(_start, temp._start);
			std::swap(_finish, temp._finish);
			std::swap(_endofstorage, temp._endofstorage);

		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		iterator begin()
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator end() const 
		{
			return _finish;
		}

		const_iterator begin() const 
		{
			return _start;
		}

		size_t size()
		{
			return _finish - _start;
		}

		size_t capacity()
		{
			return _endofstorage - _start;
		}

		void resize(size_t n,const T& val = T())
		{
			if (n > size())
			{
				reserve(n);
				while (_finish < _start + n)
				{
					*_finish = val;
					++_finish;
				}
			}
			else
			{
				_finish = _start + n;
			}
		}

		T& operator[](size_t pos)
		{
			return _start[pos];
		}

		void reserve(size_t n)
		{
			if (n > capacity())
			{
				T* temp = new T[n];
				size_t old_size = size();
				for (size_t i = 0;i < size();i++)
				{
					temp[i] = _start[i];
				}
				delete[] _start;
				_start = temp;
				_finish = _start + old_size;
				_endofstorage = _start + n;
			}
		}

		iterator insert(iterator pos, const T& val)
		{
			assert(pos >= begin() && pos <= end());

			if (_finish == _endofstorage)
			{
				size_t len = pos - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				pos = _start + len;
			}

			iterator it = end() - 1;
			while (it >= pos)
			{
				*(it + 1) = *it;
				it--;
			}

			*pos = val;
			++_finish;
			return pos;
		}

		void push_back(const T& val)
		{
			insert(end(), val);
		}

		iterator erase(iterator pos)
		{
			assert(pos >= begin() && pos < end());

			iterator it = pos+1;
			while (it < end())
			{
				*(it - 1) = *it;
				it++;
			}

			_finish--;
			return pos;
		}

		void pop_back()
		{
			erase(end() - 1);
		}

		bool empty()
		{
			return _start == _finish;
		}

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _endofstorage = nullptr;
	};

	template<typename T>
	void print_vector(const vector<T>& v)
	{
		typename vector<T>::const_iterator it = v.begin();
		while (it != v.end())
		{
			cout << *it << " ";
			it++;
		}
		cout << endl;
	}

	void test1()
	{
		vector<int> v1;
		v1.push_back(4);

		v1.push_back(1);
		v1.push_back(2);
		v1.push_back(4);

		v1.push_back(3);
		v1.push_back(4);
		v1.push_back(4);

		v1.push_back(5);
		v1.push_back(4);
		v1.push_back(4);


		v1.insert(v1.begin(), 0);
		v1.insert(v1.end(), 6);
		v1.pop_back();
		v1.erase(v1.begin());

		auto it = v1.begin();
		while(it != v1.end())
		{
			if ((*it) % 2 == 0)
			{
				it = v1.erase(it);
			}
			else
			{
				++it;
			}
		}

		print_vector(v1);
	}


}